package com.atguigu.gmall.payment.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 支付宝的客服端
 */
@Configuration
public class AlipayConfig {
    @Value("${alipay_url}")
    private String alipayUrl;

    @Value("${app_id}")
    private String appId;

    @Value("${app_private_key}")
    private String appPrivateKey;

    @Value("${alipay_public_key}")
    private String alipayPublicKey;

    @Value("${return_payment_url}")
    private String returnPaymentUrl;

    @Value("${notify_payment_url}")
    private String notifyPaymentUrl;

    /**
     * 将AlipayClient放入ioc容器
     * @return
     */
    @Bean
    public AlipayClient alipayClient(){
        return new DefaultAlipayClient(
                alipayUrl,
                appId,
                appPrivateKey,
                "json",
                "utf-8",
                alipayPublicKey,
                "RSA2");
    }

    /**
     * 将支付页面的请求对象进行ioc
     * @return
     */
    @Bean
    public AlipayTradePagePayRequest alipayTradePagePayRequest(){
        //初始化支付页面的请求对象
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl(notifyPaymentUrl);
        //同步跳转地址，仅支持http/https
        request.setReturnUrl(returnPaymentUrl);
        return request;
    }

    /**
     * 将交易查询的请求对象进行ioc
     * @return
     */
    @Bean
    public AlipayTradeQueryRequest alipayTradeQueryRequest(){
        return new AlipayTradeQueryRequest();
    }
}
